package com.ben.chatroom.session;

import com.ben.chatroom.common.entity.ImNode;
import com.ben.chatroom.distribute.PeerSender;
import com.ben.chatroom.distribute.WorkerRouter;
import com.ben.chatroom.session.entity.SessionCache;
import lombok.AllArgsConstructor;
import lombok.Builder;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 远程会话
 *
 * @author huangyijun
 */
@Builder
@AllArgsConstructor
public class RemoteSession implements ServerSession {

  private boolean valid;
  private SessionCache sessionCache;

  public RemoteSession(SessionCache cache) {
    this.sessionCache = cache;
  }


  @Override
  public void writeAndFlush(Object pkg) {
    //转发
    ImNode node = sessionCache.getNode();
    long nodeId = node.getId();

    PeerSender peerSender = WorkerRouter.getInst().router(nodeId);

    if(peerSender != null) {
      peerSender.writeAndFlush(pkg);
    }


  }

  @Override
  public String getSessionId() {
    return sessionCache.getSessionId();
  }

  @Override
  public boolean isValid() {
    return valid;
  }

  @Override
  public String getUserId() {
    return sessionCache.getUserId();
  }
}
